package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;



    /**
     * 添加spu
     * @param spuInfo
     */
    @Transactional
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        //将前端传过来的参数保存到spu_info表中
        spuInfoService.save(spuInfo);
        //将传过来的参数中spuImageList保存到spu_image表中
       List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        for (SpuImage spuImage : spuImageList) {
           spuImage.setSpuId(spuInfo.getId());
           spuImageService.save(spuImage);
        }
        //将传过来的参数中spuSaleAttrList保存到spu_sale_attr表中
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
       for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
           spuSaleAttr.setSpuId(spuInfo.getId());
           spuSaleAttrService.save(spuSaleAttr);
           //将spuSaleAttrList中spuSaleAttrValueList保存到spu_sale_attr_value表中
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
               spuSaleAttrValue.setSpuId(spuInfo.getId());


               spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
               spuSaleAttrValueService.save(spuSaleAttrValue);

           }

       }

    }
}




